package cn.stylefeng.guns.dao.service.impl;

import cn.stylefeng.guns.dao.IGaiaMaterialDocService;
import cn.stylefeng.guns.dao.entity.GaiaMaterialDoc;
import cn.stylefeng.guns.dao.entity.GaiaWmsDiaoboM;
import cn.stylefeng.guns.dao.mapper.GaiaMaterialDocMapper;
import cn.stylefeng.guns.dao.mapper.GaiaWmsDiaoboMMapper;
import cn.stylefeng.guns.dao.service.IGaiaWmsDiaoboMService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author quspacedraon
 * @since 2024-03-07
 */
@Service
@DS("yaode_sqlserver")
public class GaiaMaterialDocServiceImpl extends ServiceImpl<GaiaMaterialDocMapper, GaiaMaterialDoc> implements IGaiaMaterialDocService {

    @Override
    public List<GaiaMaterialDoc> getPurchaseList(String time) {
        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
                .eq(GaiaMaterialDoc::getMatDocDate, time)
                .eq(GaiaMaterialDoc::getClient, "10000504")
                .eq(GaiaMaterialDoc::getMatType, "CD")
                .in(GaiaMaterialDoc::getMatSiteCode, "100", "114");
        return list(cd);
    }

    @Override
    public List<GaiaMaterialDoc> getSaleList(String time) {
        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
                .eq(GaiaMaterialDoc::getMatDocDate, time)
                .eq(GaiaMaterialDoc::getClient, "10000504")
                .in(GaiaMaterialDoc::getMatType, "LS", "LX", "LT")
                .notIn(GaiaMaterialDoc::getMatSiteCode, "100", "114");
        return list(cd);
    }

    @Override
    public List<GaiaMaterialDoc> getSaleReturnList(String time) {
        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
                .eq(GaiaMaterialDoc::getMatDocDate, time)
                .eq(GaiaMaterialDoc::getClient, "10000504")
                .in(GaiaMaterialDoc::getMatType, "GD")
                .in(GaiaMaterialDoc::getMatSiteCode, "100", "114");
        return list(cd);
    }

    @Override
    /**
     * SELECT
     * MAT_BATCH,MAT_PRO_CODE,SUM(case when MAT_TYPE='XD' then CAST(MAT_QTY AS decimal) else -CAST(MAT_QTY AS decimal) end) AS 数量
     *
     * FROM GAIA_MATERIAL_DOC where client='10000504'
     *                          AND MAT_DOC_DATE >='20240401'
     *                          AND MAT_TYPE in('XD','ED')
     *                          AND MAT_SITE_CODE in(100,114)
     * group by MAT_BATCH,MAT_PRO_CODE;
     */
    public List<GaiaMaterialDoc> getBatchSaleList(String time) {

        QueryWrapper<GaiaMaterialDoc> objectQueryWrapper = Wrappers.<GaiaMaterialDoc>query()
                .select("MAT_BATCH,MAT_PRO_CODE,SUM(case when MAT_TYPE='XD' then CAST(MAT_QTY AS decimal) else -CAST(MAT_QTY AS decimal) end) AS MAT_QTY")
                .eq("client", "10000504")
                .le("MAT_DOC_DATE", time)
                //数据重新开始计算节点
                .ge("MAT_DOC_DATE", "20240601")
//                .eq("MAT_BATCH", "P20240012745")
                .in("MAT_TYPE", "XD", "ED")
                .in("MAT_SITE_CODE", "100", "114")
                .groupBy("MAT_BATCH,MAT_PRO_CODE");


//        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
//                .eq(GaiaMaterialDoc::getMatDocDate, time)
//                .eq(GaiaMaterialDoc::getClient, "10000504")
//                .in(GaiaMaterialDoc::getMatType, "XD","ED")
//                .in(GaiaMaterialDoc::getMatSiteCode, "100", "114");

        return list(objectQueryWrapper);
    }




    public List<GaiaMaterialDoc> getBatchSaleListToday(String startTime) {

        QueryWrapper<GaiaMaterialDoc> objectQueryWrapper = Wrappers.<GaiaMaterialDoc>query()
                .select("MAT_BATCH,MAT_PRO_CODE,SUM(case when MAT_TYPE='XD' then CAST(MAT_QTY AS decimal) else -CAST(MAT_QTY AS decimal) end) AS MAT_QTY")
                .eq("client", "10000504")
                .eq("MAT_DOC_DATE", startTime)
                .in("MAT_TYPE", "XD", "ED")
                .in("MAT_SITE_CODE", "100", "114")
                .groupBy("MAT_BATCH,MAT_PRO_CODE");


//        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
//                .eq(GaiaMaterialDoc::getMatDocDate, time)
//                .eq(GaiaMaterialDoc::getClient, "10000504")
//                .in(GaiaMaterialDoc::getMatType, "XD","ED")
//                .in(GaiaMaterialDoc::getMatSiteCode, "100", "114");

        return list(objectQueryWrapper);
    }

    @Override
    public List<GaiaMaterialDoc> getBatchSaleList(String startTime, String endTime) {
        QueryWrapper<GaiaMaterialDoc> objectQueryWrapper = Wrappers.<GaiaMaterialDoc>query()
                .select("MAT_BATCH,MAT_PRO_CODE,MAT_TYPE,sum(CAST(MAT_QTY AS decimal))  AS MAT_QTY")
                .eq("client", "10000504")
                .ge("MAT_DOC_DATE", startTime)
                .le("MAT_DOC_DATE", endTime)
                .in("MAT_TYPE", "XD", "ED")
                .in("MAT_SITE_CODE", "100", "114")
                .groupBy("MAT_BATCH,MAT_PRO_CODE,MAT_TYPE");


//        LambdaQueryWrapper<GaiaMaterialDoc> cd = Wrappers.<GaiaMaterialDoc>lambdaQuery()
//                .eq(GaiaMaterialDoc::getMatDocDate, time)
//                .eq(GaiaMaterialDoc::getClient, "10000504")
//                .in(GaiaMaterialDoc::getMatType, "XD","ED")
//                .in(GaiaMaterialDoc::getMatSiteCode, "100", "114");

        return list(objectQueryWrapper);
    }
}
